package helloworld.admin.action.center;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import helloworld.admin.bean.CenterAdminBean;
import helloworld.admin.form.center.*;
import helloworld.delegate.center.CenterDelegate;
import helloworld.delegate.center.CenterListDelegate;

public class ManageCentersAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ActionForward forward = null;
		ManageCentersForm custform = (ManageCentersForm)form;
		
		String strAction = request.getParameter("submitAction");
		
		if(strAction.equals("search")){
			CenterListDelegate delegate = new CenterListDelegate();
			CenterAdminBean[] customers = delegate.findCenters(custform.getLastName());
			request.setAttribute("CUSTOMER_SUMMARY_OBJECTS", customers);
			forward = mapping.findForward("success");
		}
		
		if(strAction.equals("delete")){
			CenterDelegate delegate = new CenterDelegate();
			String[] idsToDelete = custform.getIdSelections();
			if (idsToDelete != null && idsToDelete.length > 0 )
			{
				for (int i=0;i<idsToDelete.length;i++)
				{
					int x = Integer.parseInt(idsToDelete[i]);
					delegate.deleteCenter(x);
				}
			}
			forward = mapping.findForward("deleteSuccess");
		}
		
		if(strAction.equals("new")){	
			forward = mapping.findForward("add");
		}
		
		return forward;
	}
}
